package com.wiscamp.ninechapters.panels.infrastructure.mappers;

import com.wiscamp.ninechapters.panels.infrastructure.po.RolePermissionPO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public interface RolePermissionMapper extends JpaRepository<RolePermissionPO, Long>, JpaSpecificationExecutor<RolePermissionPO> {
    @Query(value = "select p from RolePermissionPO p where p.roleId = :roleId and p.permissionId = :permissionId")
    RolePermissionPO findByRoleAndPermission(long roleId, long permissionId);

    @Query(value = "select p.permissionId from RolePermissionPO p where p.roleId = :roleId")
    Collection<Long> findPermissionIdsByRole(long roleId);

    @Query(value = "select distinct p.permissionId from RolePermissionPO p where p.roleId in (:roleIds)")
    Collection<Long> findPermissionIdsByRoles(Collection<Long> roleIds);
}
